package com.bookyourtrek.util;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

/**
 * @author /av
 * Class for creating connection to database
 */
public class DbConnection {

        static Connection conn; 
        
        /**
         * @author /av
         * @return
         *  It uses the runtimeSetting and Loads the driver and 
         *  makes the connection for the first time 
         *  if connection is unavailable
         */
        public static Connection getConnection(){
                if(conn == null){
                        try{
                                Class.forName(RuntimeSettings.driverClass).newInstance();
                                conn = (Connection) DriverManager.getConnection(RuntimeSettings.dbUrl + RuntimeSettings.databaseName,
                                                RuntimeSettings.dbUserID, RuntimeSettings.dbPassword);
                                //conn.setAutoCommit(false);
                        }
                        catch(Exception ex){
                                ex.printStackTrace();
                        }
                }
                return conn;
        }
        
         /**
         * @author /av
         * @return
         * closing SQL connection 
         * @throws SQLException
         */
        public void removeConnection() throws SQLException
            {
                conn.close();
                conn = null;
            }
}